#!/usr/bin/env perl
use warnings;
use strict;

use lib 'lib';
use Guile::Space;

my $gs = Guile::Space->new;



my $ary_res = $gs->run(
    code  => q/(foo 'get 1)/,
    scope => {
        foo => [1 .. 3],
    },
);
print "ArrayResult<$ary_res>\n";



exit;
my $addupper = Guile::eval_str(q/ (lambda (ls) (apply + ls)) /);
print $addupper->( [1, 2, 3] ), "\n";


exit;
my $hash_res = $gs->run(
    code  => q/((foo 'get 'double) (foo 'get 'fooval))/,
    scope => {
        foo => { fooval => 23, double => sub { $_[0] * 2 } },
    }
);
print "HashResult<$hash_res>\n";







exit;
my $bar = $gs->run(
    code  => q/ ((foo 'double) 'bar baz) /,
    scope => {
        foo => FooTest->new(2),
        baz => 23,
    }
);

print "Result<$bar>\n";

{   package FooTest;
    sub new { return bless { x => $_[1] } => $_[0] }
    sub bar { my $self = shift; return $self->{x} * shift }
    sub double { my $self = shift; return ref($self)->new($self->{x} * 2) }
}








exit;
my $res = $gs->run(
    code  => '(+ (baz (bar)) 2 3)',
    scope => {
        foo => 5,
        bar => sub { 6 },
        baz => sub { $_[0] * 2 },
    },
);
print "EndResult: $res\n";



print "***\n";
print "*** NEXT TRY\n";
print "***\n";

my $foo = $gs->run(
    scope => {  perl_join => sub { join ', ', @_ },
                add       => sub { $_[0] + $_[1] },
                sum       => sub { my $x; my @y = split //, $_[0]; $x += $_ for @y; $x },
                h4x0r     => sub { $_[0] =~ tr/Sio/510/; $_[0] },
                upper     => sub { uc $_[0] },
                hop       => sub { sub { "Foobaresc: $_[0]!" } },
             },
    code  => q(
        (perl_join (+ (add 2 3)
                      (sum 123))
                   (h4x0r "Skiddoo")
                   (upper "fnord!")
                   ((hop) "Certainly"))
    ),
);
print "Complex EndResult: $foo\n";


#   RELOOPINGS
#       Eine Procedure muss umbgebaut werden zu
#       my $gs_container = Container mit weak container space ref
#       sub { $gs_container->gs->cycle_dings( ... aufrufen der originalen sub als entrypoint );
#
#       refaddr speichern und original verwenden, wenn rueckgabewert coderef und gleich
#
